//创建监听元素对象
var resultobserverweb = {
    TypeId: 2,
    Type: "",
    Message: "",
    Html: "",
    Text: "",
    DataId: "",
}
var resultobserverweb2 = {
    TypeId: 2,
    Type: "",
    Message: "",
    Html: "",
    Text: "",
    DataId: "",
}
var resultobserverweb3 = {
    TypeId: 2,
    Type: "",
    Message: "",
    Html: "",
    Text: "",
    DataId: "",
}
var resultobserverweb4 = {
    TypeId: 2,
    Type: "",
    Message: "",
    Html: "",
    Text: "",
    DataId: "",
}
var leftid1 = 0;
var leftNum1 = 0;
var leftid2 = 0;
var leftNum2 = 0;
//生成随机数
function generateSixDigitRandomNumber() {
    return Math.floor(Math.random() * 900000) + 100000;
}
//消息内容解析
var targetNode = document.getElementsByClassName("webcast-chatroom___items")[0].children[0];
var options = { attributes: true, childList: true, subtree: false, attributeOldValue: false };
function callback(mutationsList, observer) {
    if (mutationsList[mutationsList.length - 1].addedNodes.length > 0) {
        resultobserverweb.Type = "info";
        resultobserverweb.Text = mutationsList[mutationsList.length - 1].addedNodes[0].innerText;
        resultobserverweb.Html = mutationsList[mutationsList.length - 1].addedNodes[0].innerHTML.replace("\"", "'").replace("\\\\\"", "'");
        resultobserverweb.DataId = mutationsList[mutationsList.length - 1].addedNodes[0].dataset.id;
        window.chrome.webview.postMessage(JSON.stringify(resultobserverweb));
    };
};
var mutationObserver = new MutationObserver(callback);
mutationObserver.observe(targetNode, options);

//礼物内容解析
var options1 = { attributes: true, childList: true, subtree: true, attributeOldValue: false };
//左1
var targetNode2 = document.getElementsByClassName('ljM5iqdR')[0];
function callback2(mutationsList, observer) {
    var liwu = ''; var html = '';
    resultobserverweb2.Type = "";
    for (var i = 0; i < mutationsList.length; i++) {
        if (mutationsList[i].addedNodes.length > 0) {
            resultobserverweb2.Type = "liwu";
            leftid1 = generateSixDigitRandomNumber();
            mutationsList[i].addedNodes[0].id = leftid1;
            if (mutationsList[i].addedNodes[0].getElementsByClassName('dnmQgQio').length == 0) {
                leftNum1 = "1";
            } else {
                leftNum1 = mutationsList[i].addedNodes[0].getElementsByClassName('dnmQgQio')[0].innerText;
            }
            liwu = mutationsList[i].addedNodes[0].innerText;
            html = html + mutationsList[i].addedNodes[0].innerHTML;
            break;
        } else if (mutationsList[i].type == "attributes") {
            var newNum1 = "";
            if (mutationsList[i].target.parentNode.getElementsByClassName('dnmQgQio').length > 0) {
                newNum1 = mutationsList[i].target.parentNode.getElementsByClassName('dnmQgQio')[0].innerText;
            }
            if (mutationsList[i].target.parentNode.id == leftid1 && leftNum1 != newNum1 && newNum1) {
                resultobserverweb2.Type = "liwu";
                leftNum1 = newNum1;
                liwu = mutationsList[i].target.parentNode.innerText;
                html = mutationsList[i].target.parentNode.innerHTML;
            } else {
                return;
            }
            break;
        } else {
            return;
        }
    }
    if (mutationsList.length > 0) {
        if (resultobserverweb2.Type == 'liwu') {
            resultobserverweb2.Text = liwu;
            resultobserverweb2.Html = html.replace("\"", "'").replace("\\\\\"", "'");
            window.chrome.webview.postMessage(JSON.stringify(resultobserverweb2));
        }
    }
};
var mutationObserver2 = new MutationObserver(callback2);
mutationObserver2.observe(targetNode2, options1);
//左2
var targetNode3 = document.getElementsByClassName('ljM5iqdR')[1];
function callback3(mutationsList, observer) {
    var liwu = ''; var html = '';
    for (var i = 0; i < mutationsList.length; i++) {
        if (mutationsList[i].addedNodes.length > 0) {
            resultobserverweb3.Type = "liwu";
            leftid2 = generateSixDigitRandomNumber();
            mutationsList[i].addedNodes[0].id = leftid2;
            if (mutationsList[i].addedNodes[0].getElementsByClassName('dnmQgQio').length == 0) {
                leftNum2 = "1";
            } else {
                leftNum2 = mutationsList[i].addedNodes[0].getElementsByClassName('dnmQgQio')[0].innerText;
            }
            liwu = mutationsList[i].addedNodes[0].innerText;
            html = html + mutationsList[i].addedNodes[0].innerHTML;
            break;
        } else if (mutationsList[i].type == "attributes") {
            var newNum2 = "";
            if (mutationsList[i].target.parentNode.getElementsByClassName('dnmQgQio').length > 0) {
                newNum2 = mutationsList[i].target.parentNode.getElementsByClassName('dnmQgQio')[0].innerText;
            }
            if (mutationsList[i].target.parentNode.id == leftid2 && leftNum2 != newNum2 && newNum2) {
                resultobserverweb3.Type = "liwu";
                leftNum2 = newNum2;
                liwu = mutationsList[i].target.parentNode.innerText;
                html = mutationsList[i].target.parentNode.innerHTML;
            } else {
                return;
            }
            break;
        } else {
            return;
        }
    }
    if (mutationsList.length > 0) {
        if (resultobserverweb3.Type == 'liwu') {
            resultobserverweb3.Text = liwu;
            resultobserverweb3.Html = html.replace("\"", "'").replace("\\\\\"", "'");
            window.chrome.webview.postMessage(JSON.stringify(resultobserverweb3));
        }
    }
};
var mutationObserver3 = new MutationObserver(callback3);
mutationObserver3.observe(targetNode3, options1);

//进入直播间和点赞消息
var targetNode4 = document.getElementsByClassName("webcast-chatroom___items")[0].children[1];
var options4 = { attributes: false, childList: true, subtree: false, attributeOldValue: false };
function callback4(mutationsList, observer) {
    if (mutationsList[mutationsList.length - 1].addedNodes.length > 0) {
        resultobserverweb4.Type = "likeinfo";
        resultobserverweb4.Text = mutationsList[mutationsList.length - 1].target.innerText;
        resultobserverweb4.Html = mutationsList[mutationsList.length - 1].target.innerHTML
        resultobserverweb4.DataId = mutationsList[mutationsList.length - 1].target.children[0].dataset.id;
        window.chrome.webview.postMessage(JSON.stringify(resultobserverweb4));
    };
};
var mutationObserver4 = new MutationObserver(callback4);
mutationObserver4.observe(targetNode4, options4);